<?php
/**
*fxapi.php 功能:提供分销直帐 api接口
*@date:2018年01月06日
*@author:jiangshilin 
*/
class Controller_Fxapi extends Controller
{
    private $input;

    public function before(){
        //传递格式未确认校验先屏蔽
        $this->input = json_decode(file_get_contents('php://input'),true);
       
    }
    /**
     * 分销只帐更新供应商sku单价
     * 分销系统传递参数：供应商ID、供应商名称、公司SKU、单价、修改时间
vmi系统修改：该供应商warehouseorders表所有未完全出库的入库单数据的单价，更新vmi_supplier_skus表该供应商对应的sku单价
     * 入参：
     * {
        "supplier_id": "供应商id",
        "supplier":"供应商名称",
        "sku": "公司sku",
        "price": "单价",
        "update_time": "修改时间",

        }

     * @Author   jiangshilin
     * @DateTime 2018-01-06
     */
    public function Action_updateSupplierSkuPrice()
    {
        if(empty($this->input)){
            $this->getMsg(401);
        }
        $data = $this->input;
    	$request['request'] = file_get_contents('php://input');
        $request['supplier_id'] = $supplier_id = $data['supplier_id'];
        $request['sku'] = $sku = $data['sku'];
        $request['price'] = $price = $data['price'];
        $request['res_status'] = 1;
        $request['res_msg'] = '';
        try{
            DB::insert('warehouseorders_fx_msg',array_keys($request))
                        ->values($request)
                        ->execute('newerp');
        }catch(Database_Exception $e){
            $this->getMsg(400,$e->getMessage());
       }
        
        //更新所有该供应商下sku下有库存的入库单价
        $sql = "UPDATE warehouseorders SET single_price=$price WHERE supplier_id=$supplier_id AND sku='$sku' AND warehouse_quantity-use_quantity>0 " ;
        $res1 = DB::query(Database::UPDATE,$sql)->execute('newerp');

        //更新供应商sku单价
        $sql = "UPDATE vmi_supplier_skus SET price=$price WHERE sku='$sku' AND supplier_id=$supplier_id";
        $res2 = DB::query(Database::UPDATE,$sql)->execute('newerp');
        if(empty($res1) && empty($res2)){
            $this->getMsg(402);
        }
        $this->getMsg(200,$result);
    }

    
    
    //获取或返回提示信息
    public function getMsg($key,$data = array(),$end=true){
        $array = array(
            //公共
            '200'=>array(
                'code'=>$key,
                'msg'=>'成功'
            ),
            '400'=>array(
                'code'=>$key,
                'msg'=>'失败'
            ),
            '401'=>array(
                'code'=>$key,
                'msg'=>'数据格式错误'
            ),
            '402'=>array(
                'code'=>$key,
                'msg'=>'供应商对应SKU在vmi无数据更新'
            ),
            
        );
        if($end){
            $this->endResult($array[$key],$data,$key);
        }else{
            return $array[$key];
        }
    }
    //输出提示信息
    public function endResult($msg,$data,$status){
        if(empty($data)){
            echo json_encode($msg);
            exit;
        }else{
            $msg = array_merge($msg,array('data'=>$data));
            echo json_encode($msg);
            exit;
        }
        
    }
    
}
